package j2025.j03;

/**
 * @Author 猫大仙
 */
public class j0318 {
    public ListNode reverseKGroup(ListNode head, int k) {
        int n = 0;
        ListNode tmp = head;
        while (tmp != null){
            n++; tmp = tmp.next;
        }
        n /= k;
        ListNode ret = new ListNode(-1), prev = ret;
        ListNode cur = head;
        for (int i = 0; i < n; i++) {
            ListNode t = cur;
            for (int j = 0; j < k; j++) {
                ListNode next = cur.next;
                cur.next = prev.next;
                prev.next = cur;
                cur = next;
            }
            prev = t;
        }
        prev.next = cur;
        return ret.next;
    }
}
